const moment = require('moment');
const userController = require('./controller/user');
const superUserController = require('./controller/superuser');
const notificationController = require('./controller/notification');
const dutyShiftController = require('./controller/dutyShift.js');
const workingScheduleController = require('./controller/workingSchedule');
const inspectionController = require('./controller/inspection');
const requestAndApprovalController = require('./controller/requestAndApproval');

const auth = require('./middleware/auth');

module.exports = (router) => {
  router.use(async (ctx, next) => {
    try{
      await next();
    } catch(err) {
      console.error(err);
    }
  });
  router.prefix('/api');

  router.get('/date', (ctx) => {
    ctx.body = {
      date: moment()
    }
  });

  /**
   * 登录和个人信息接口
   */
  router
    .post('/login', userController.login)
    .get('/personalData', auth(), userController.personalData);

  /**
   * 首页公告页面接口
   */
  router
    .post('/notification/list', notificationController.getNotificationList)
    .post('/notification/add', auth('dashboardEdit'), notificationController.addNotification)
    .post('/notification/update', auth('dashboardEdit'), notificationController.updateNotification)
    .post('/notification/delete', auth('dashboardEdit'), notificationController.deleteNotification)
    .post('/dashboard/dutyList', auth('dashboardView'), notificationController.getDutyList)
    .post('/dashboard/equipmentState', notificationController.getEquipmentState);

  /**
   * 文件分享接口
   */
  router
    .post('/fileShare/list', auth('fileShareView'), notificationController.getFileList)
    .post('/fileShare/add', auth('fileShareEdit'), notificationController.addFile)
    .post('/fileShare/update', auth('fileShareEdit'), notificationController.updateFile)
    .post('/fileShare/delete', auth('fileShareEdit'), notificationController.deleteFile);

  /**
   * 排班表模块
   */
  router
    .get('/teamMember/list', workingScheduleController.getTeamMember)
    .post('/workingSchedule/list', auth('workingScheduleView'), workingScheduleController.getScheduleList)
    .post('/leader/workingSchedule/add', auth('workingScheduleView'), workingScheduleController.addSchedule)
    .post('/leader/workingSchedule/update', auth('workingScheduleView'), workingScheduleController.updateSchedule)
    .post('/leader/workingSchedule/delete', auth('workingScheduleView'), workingScheduleController.deleteSchedule)
    .post('/workingSchedule/export', auth('workingScheduleView'), workingScheduleController.getExportData);

  /**
   * 超级管理员设备管理模块
   */
  router
    .post('/equipment/list', superUserController.getEquipmentList)  // 由于其他页面需要读取到设备列表，因此放开权限
    .post('/superuser/equipment/add', auth('equipmentControlView'), superUserController.addEquipment)
    .post('/superuser/equipment/update', auth('equipmentControlView'), superUserController.updateEquipment)  // 由于修改了设备巡视检查单那里会出很多问题，所以不允许修改
    .post('/superuser/equipment/delete', auth('equipmentControlView'), superUserController.deleteEquipment)
    .post('/superuser/equipment/stop', auth('equipmentControlView'), superUserController.stopEquipment);

  /**
   * 超级管理员部门管理模块
   */
  router
    .post('/superuser/group/list', superUserController.getGroupList)
    .post('/superuser/group/add', auth('groupControlView'), superUserController.addGroup)
    .post('/superuser/group/update', auth('groupControlView'), superUserController.updateGroup)
    .post('/superuser/group/delete', auth('groupControlView'), superUserController.deleteGroup)
    .post('/superuser/group/privilege/set', auth('groupControlView'), superUserController.setGroupPrivilege);

  /**
   * 超级管理员用户管理模块
   */
  router
    .post('/superuser/user/list', auth('userControlView'), superUserController.getUserList)
    .post('/superuser/user/add', auth('userControlView'), superUserController.addUser)
    .post('/superuser/user/update', auth('userControlView'), superUserController.updateUser)
    .post('/superuser/user/start', auth('userControlView'), superUserController.startUser)
    .post('/superuser/user/stop', auth('userControlView'), superUserController.stopUser)
    .post('/superuser/user/delete', auth('userControlView'), superUserController.deleteUser)
    // .post('/superuser/privilege/set', auth('userControlView'), superUserController.setUserBrowse)
    .post('/superuser/approval/set', auth('userControlView'), superUserController.setUserApproval);

  /**
   * 个人设置页面
   */
  router.post('/staff/changeInfo', auth('settingsView'), userController.changeUserInfo);

  /**
   * 交接班记录模块
   */
  router
    .post('/dutyShift/list', auth('dutyShiftView'), dutyShiftController.getDutyShiftList)
    .post('/dutyShift/add', auth('dutyShiftView'), dutyShiftController.addDutyShift)
    .post('/dutyShift/update', auth('dutyShiftView'), dutyShiftController.updateDutyShift)
    .post('/dutyShift/delete', auth('dutyShiftView'), dutyShiftController.deleteDutyShift)
    .post('/dutyShift/analyze', auth('dutyShiftView'), dutyShiftController.analysisFile)
    .post('/dutyShift/import', auth('dutyShitView'), dutyShiftController.importData);

  /**
   * 巡视检查单模块
   */
  router
    .post('/inspectionList/list', auth('inspectionListView'), inspectionController.getInsFileList)
    .post('/inspectionList/add', auth('inspectionListView'), inspectionController.addInsFile)
    .post('/inspectionList/delete', auth('inspectionListView'), inspectionController.deleteInsFile)
    .post('/inspectionRecord/day/content', auth('inspectionListView'), inspectionController.getDayInspectionRecordList)
    .post('/inspectionRecord/week/content', auth('inspectionListView'), inspectionController.getWeekInspectionRecordList)
    .post('/inspectionRecord/update', auth('inspectionListView'), inspectionController.updateInspectionData)
    .post('/inspectionRecord/abnormal/add', auth('inspectionListView'), inspectionController.addAbnormalRecord)
    .post('/inspectionRecord/confirm', auth('inspectionListView'), inspectionController.confirmRecord);

  /**
   * 文件上传
   */
  router.post('/file/upload', auth(), userController.fileUpload);

  /**
   * 申请与审批模块
   */
  router
    .post('/staff/requestNote/list', auth(), requestAndApprovalController.getRequestNoteList)
    .post('/staff/requestNote/content', auth(), requestAndApprovalController.getRequestNoteContent)
    .post('/staff/requestNote/commit', auth(), requestAndApprovalController.addOrUpdateRequestNote)
    .post('/staff/requestNote/reset', auth(), requestAndApprovalController.resetRequestNote)
    .post('/approver/requestNote/approval', auth(), requestAndApprovalController.approvalRequestNote)
    .post('/staff/requestNote/close', auth(), requestAndApprovalController.closeRequestNote)
    .post('/requestChange/review/addOnSiteDescription', auth(), requestAndApprovalController.requestChangeAddOnSiteDescription)
    // .post('/requestChange/review/verify', auth(), requestAndApprovalController.requestChangeVerify)
    .post('/requestChange/review/addReview' , auth(), requestAndApprovalController.requestChangeAddReview);

  /**
   * 消息通知模块
   */

  router
    .get('/staff/message/list', auth(), userController.getMessage)
    .post('/staff/message/delete', auth(), userController.deleteMessage);
};

